package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.ProductMaintain;
import com.spice.service.mapper.ProductMaintainMapper;
import com.spice.service.service.ProductMaintainService;
import org.springframework.stereotype.Service;

@Service
public class ProductMaintainServiceImpl extends ServiceImpl<ProductMaintainMapper, ProductMaintain> implements ProductMaintainService {
    @Override
    public Response saveProductMaintain(ProductMaintain newProductMaintain) {
        if (CheckUtil.objectsHasNull(
                newProductMaintain.getSerialName(),
                newProductMaintain.getProductName(),
                newProductMaintain.getUpdateReason(),
                newProductMaintain.getMaintainer()
        ))
        {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //检查是否与同意系列中已有产品名称冲突
        LambdaQueryWrapper<ProductMaintain> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductMaintain::getSerialName,newProductMaintain.getSerialName());
        wrapper.eq(ProductMaintain::getProductName,newProductMaintain.getProductName());
        if (this.count(wrapper) > 0)
        {
            throw new SpiceException().message("该系列已有此产品维护信息");
        }
        if (this.save(newProductMaintain)) {
            return Response.ok().message("产品维护信息添加成功").data("data",newProductMaintain);
        }
        else
        {
            return Response.error().message("产品维护信息添加失败");
        }
    }

    @Override
    public Response removeProductMaintain(Long id) {
        if (CheckUtil.objectsHasNull(id))
        {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认要删除的是否存在
        ProductMaintain removedProductMaintain = this.getById(id);
        if (CheckUtil.objectsHasNull(removedProductMaintain))
        {
            throw new SpiceException().message("待删除的产品维护信息不存在");
        }
        if (this.removeById(id)) {
            return Response.ok().message("产品维护信息删除成功").data("data",removedProductMaintain);
        }
        else {
            return Response.error().message("产品维护信息删除失败");
        }
    }

    @Override
    public Response updateProductMaintain(Long id, ProductMaintain newProductMaintain) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id,newProductMaintain))
        {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认要更新的是否存在
        ProductMaintain updatedProductMaintain = this.getById(id);
        if (CheckUtil.objectsHasNull(updatedProductMaintain))
        {
            throw new SpiceException().message("待维护的产品维护信息不存在");
        }
        //更改后的产品名称是否与同系列的其他产品名称重名
        if (!updatedProductMaintain.getProductName().equals(newProductMaintain.getProductName()))
        {
            LambdaQueryWrapper<ProductMaintain> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProductMaintain::getSerialName,newProductMaintain.getSerialName());
            wrapper.eq(ProductMaintain::getProductName,newProductMaintain.getProductName());
            if (this.count(wrapper) > 0)
            {
                throw new SpiceException().message("该系列已有此产品维护信息");
            }
        }
        //更新待更新的数据id和createTime
        newProductMaintain.setId(id);
        newProductMaintain.setCreateTime(updatedProductMaintain.getCreateTime());
        if (this.updateById(newProductMaintain))
        {
            return Response.ok().message("产品维护数据更新成功").data("data",newProductMaintain);
        }
        else {
            return Response.error().message("产品维护数据更新失败");
        }
    }
}
